package com.oa.tools.jobs.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Node;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.OrgElement;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.StringUtil;
import com.oa.tools.utils.XmlUtil;
import com.oa.tools.utils.XmlUtil.XMLHandler;

public class UpdatePersonDeptTmpTask extends FlowHandlerTaskBase{

	public UpdatePersonDeptTmpTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
		// TODO Auto-generated constructor stub
		
		this.flowStatus = FlowHandlerTaskBase.FLOW_STATUS_DONE;
		this.templateId = "160719f9ae446c61ff4c57247a9a007f";
		
		//,ProcessBase.FLOW_STATUS_DONE, "人员借调办理通知", new String[] {"借调"})
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		// TODO Auto-generated method stub
		final Map<String,Object> resultMap = new HashMap<>();
		String xmlStr = obj.getExtendDataXml();
//		System.out.println(xmlStr);
		
		String personObjId = "fd_34375e1ddf4efc";
		String pos_old_id = "fd_348f629018f726";
		String pos_id = "fd_348f62a328b316";
		String dept_id = "fd_34375e5e30f8a4";
		
		
		XmlUtil.parseXmlWithHandler(xmlStr, "/java/object/void",new XMLHandler() {
			@Override
			public void hanle(Node paramNode) throws Exception {
				Node ele1 = paramNode.selectSingleNode("string");
				if(personObjId.equals(ele1.getText())) {
					Node personIdNode= paramNode.selectSingleNode("object/void/string[2]");
					String personId = personIdNode.getText();
					resultMap.put("personId", personId);
				}else if(pos_old_id.equals(ele1.getText())) {
					Node posOldNode= paramNode.selectSingleNode("string[2]");
					String oldPosition = posOldNode.getText();
					resultMap.put("oldPosition", oldPosition);
				}else if(pos_id.equals(ele1.getText())) {
					Node posNode= paramNode.selectSingleNode("string[2]");
					String newPosition = posNode.getText();
					resultMap.put("newPosition", newPosition);
				}else if(dept_id.equals(ele1.getText())) {
					Node deptIdNode= paramNode.selectSingleNode("object/void/string[2]");
					String deptId = deptIdNode.getText();
					resultMap.put("deptId", deptId);
				}
			}
			
		});
		String personId = (String) resultMap.get("personId");
		String deptId = (String) resultMap.get("deptId");
		String oldPosition = (String) resultMap.get("oldPosition");
		String newPosition = (String) resultMap.get("newPosition");
		
		OrgElement personEle = orgElementDAO.selectByPrimaryKey(personId);
		
		String content = null;
		if(StringUtil.isBlank(personEle.getFdParentid())) {
			content+=personEle.getFdName()+"的部门ID为空";
		}
		OrgElement oldDept = orgElementDAO.selectByPrimaryKey(personEle.getFdParentid());
		OrgElement newDept = orgElementDAO.selectByPrimaryKey(deptId);
		
		
		if(!personEle.getFdParentid().equals(deptId)) {
			personEle.setFdParentid(deptId);
			personEle.setFdAlterTime(new Date());
			orgElementDAO.updateByPrimaryKeySelective(personEle);
			content = personEle.getFdName()+"从部门："+oldDept.getFdName()+"的"+oldPosition+"，临时借调到部门："+newDept.getFdName()+"的"+newPosition;
		}
		
		if(oldPosition.contains("城市经理") 
				||oldPosition.contains("城市经理")||newPosition.contains("城市经理")
				||oldPosition.contains("部门经理")||newPosition.contains("部门经理")
				||oldPosition.contains("部门副经理")||newPosition.contains("部门副经理")
				||oldPosition.contains("总监")||newPosition.contains("总监")) {
			content +="\r\n---------------注意维护岗位人员";
		}
		
		return content;
	}

	
}
